package com.ifinc.common.web.controller;

import com.ifinc.common.web.BaseRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: luotao
 * Date: 13-10-28
 * Time: 下午9:30
 * To change this template use File | Settings | File Templates.
 */
public class CoreController {
    static final Logger log = LoggerFactory.getLogger(CentralControler.class);

    /**
     * 提取request中的参数，转化成Map返回
     *
     * @param req
     * @return
     */
    public Map<String, String> makeParamMap(HttpServletRequest req) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        Enumeration e = req.getParameterNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = req.getParameter(key);
            map.put(key, value);
        }
        log.debug("request param:" + map);
        return map;
    }

    /**
     * 提取reqest的Header中的参数，转化成Map返回
     *
     * @param req
     * @return
     */
    public Map<String, String> makeHeaderParam(HttpServletRequest req) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        Enumeration e = req.getHeaderNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = req.getHeader(key);
            map.put(key, value);
        }
        log.debug("request header:" + map);
        return map;
    }

    public void putMap2ReqAttr(Map<String, Object> result, HttpServletRequest req) {
        for (Map.Entry<String, Object> entry : result.entrySet()) {
            req.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    public Map doAction(BaseRequest baseRequest) {

        baseRequest.verify();
        baseRequest.setHeader(null);
        baseRequest.setParam(null);
        baseRequest.doAction();
        return null;
    }
}
